﻿Public Class ClassDiapo
    'Diapositive Visio
    Public ID As Integer
    Public Titre As String
    Public PageName As String
    'ViewRect
    Public vTop, vLeft, vWidth, vHeight As Double
    'Window
    'Public wTop, wLeft, wWidth, wHeight As Integer
    'Interface Visio
    'Public WindowState As Integer
    Public ShowRulers As Integer
    Public ShowGrid As Short
    Public ShowScrollBars As Integer
    Public ShowGuides As Integer
    Public ShowToolbar As Integer
    Public ShowPageTab As Integer
    'Calques
    Public Calques As ColCalques
    'Captures
    Private FullImage As System.Drawing.Image
    Private ThumbnailImage As System.Drawing.Image

    Public Sub New(ByVal iID As Integer)
        Me.ID = iID
        Me.Titre = ""
        Me.PageName = ""
        Me.vTop = 0
        Me.vLeft = 0
        Me.vWidth = 1
        Me.vHeight = 1
        Me.ShowRulers = -1
        Me.ShowGrid = -1
        Me.ShowScrollBars = -1
        Me.ShowGuides = -1
        Me.ShowToolbar = -1
        Me.ShowPageTab = -1
        Me.FullImage = Nothing
        Me.ThumbnailImage = Nothing
        Me.Calques = New ColCalques
    End Sub

    Public Function ToXMLElement(ByRef oXmlDoc As System.Xml.XmlDocument) As System.Xml.XmlElement
        Dim oDiapoXML As System.Xml.XmlElement = oXmlDoc.CreateElement("Diapo")
        AjoutePropXML(oXmlDoc, oDiapoXML, "ID", Me.ID)
        AjoutePropXML(oXmlDoc, oDiapoXML, "Titre", Me.Titre)
        AjoutePropXML(oXmlDoc, oDiapoXML, "PageName", Me.PageName)
        AjoutePropXML(oXmlDoc, oDiapoXML, "vTop", Me.vTop.ToString)
        AjoutePropXML(oXmlDoc, oDiapoXML, "vLeft", Me.vLeft.ToString)
        AjoutePropXML(oXmlDoc, oDiapoXML, "vWidth", Me.vWidth.ToString)
        AjoutePropXML(oXmlDoc, oDiapoXML, "vHeight", Me.vHeight.ToString)
        AjoutePropXML(oXmlDoc, oDiapoXML, "ShowRulers", Me.ShowRulers.ToString)
        AjoutePropXML(oXmlDoc, oDiapoXML, "ShowGrid", Me.ShowGrid.ToString)
        AjoutePropXML(oXmlDoc, oDiapoXML, "ShowScrollBars", Me.ShowScrollBars.ToString)
        AjoutePropXML(oXmlDoc, oDiapoXML, "ShowGuides", Me.ShowGuides.ToString)
        AjoutePropXML(oXmlDoc, oDiapoXML, "ShowToolbar", Me.ShowToolbar.ToString)
        AjoutePropXML(oXmlDoc, oDiapoXML, "ShowPageTab", Me.ShowPageTab.ToString)
        For Each oCalque As ClassCalque In Me.Calques
            oDiapoXML.AppendChild(oCalque.ToXMLElement(oXmlDoc))
        Next
        Return oDiapoXML
    End Function

    Public Sub FromXMLElement(ByVal oElement As System.Xml.XmlElement)
        Me.ID = GetPropXMLINT(oElement, "ID")
        Me.Titre = GetPropXML(oElement, "Titre")
        Me.PageName = GetPropXML(oElement, "PageName")
        Me.vTop = GetPropXMLDBL(oElement, "vTop")
        Me.vLeft = GetPropXMLDBL(oElement, "vLeft")
        Me.vWidth = GetPropXMLDBL(oElement, "vWidth")
        Me.vHeight = GetPropXMLDBL(oElement, "vHeight")
        Me.ShowRulers = GetPropXMLINT(oElement, "ShowRulers")
        Me.ShowGrid = GetPropXMLINT(oElement, "ShowGrid")
        Me.ShowScrollBars = GetPropXMLINT(oElement, "ShowScrollBars")
        Me.ShowGuides = GetPropXMLINT(oElement, "ShowGuides")
        Me.ShowToolbar = GetPropXMLINT(oElement, "ShowToolbar")
        Me.ShowPageTab = GetPropXMLINT(oElement, "ShowPageTab")
        Dim oNodeList As System.Xml.XmlNodeList = oElement.GetElementsByTagName("Calque")
        Me.Calques.Clear()
        For Each oNode As System.Xml.XmlNode In oNodeList
            Dim oCalques As New ClassCalque
            oCalques.FromXMLElement(oNode)
            Me.Calques.add(oCalques)
        Next
    End Sub

    Public Function GetImage() As System.Drawing.Image
        Return Me.FullImage
    End Function

    Public Function GetThumbnail() As System.Drawing.Image
        Return Me.ThumbnailImage
    End Function

    Public Sub Capturer()
        'Capturer l'état de l'affichage Visio pour pouvoir le réappliquer au besoin
        If Not oApp Is Nothing Then
            Me.ShowToolbar = oApp.ShowToolbar
            If Not oApp.ActiveWindow Is Nothing Then
                System.Windows.Forms.Application.DoEvents()
                'Positions de la fenêtre active
                'Éléments d'interface
                Me.ShowRulers = oApp.ActiveWindow.ShowRulers
                Me.ShowGrid = oApp.ActiveWindow.ShowGrid
                Me.ShowScrollBars = oApp.ActiveWindow.ShowScrollBars
                Me.ShowGuides = oApp.ActiveWindow.ShowGuides
                Me.ShowPageTab = oApp.ActiveWindow.ShowPageTabs
                'Page active
                Me.PageName = oApp.ActiveWindow.Page.Name
                If Me.Titre = "" Then Me.Titre = "Diapo " & Me.ID.ToString & " (" & Me.PageName & ")"
                'Position de l'affichage à l'écran
                oApp.ActiveWindow.GetViewRect(Me.vLeft, Me.vTop, Me.vWidth, Me.vHeight)
                'Calques
                Me.Calques.Clear()
                For Each oLayer As Microsoft.Office.Interop.Visio.Layer In oApp.ActivePage.Layers
                    Dim oCalque As New ClassCalque
                    oCalque.Nom = oLayer.Name
                    oCalque.Visible = oLayer.CellsC(visLayerVisible).FormulaU <> "0"
                    Me.Calques.add(oCalque)
                Next
                'Mettre à jour la capture écran et la vignette
                Call Me.UpdateImages()
            End If
        End If
    End Sub

    Public Sub UpdateImages()
        'Mettre à jour la capture écran et la vignette de la diapo
        If Not oApp Is Nothing Then
            Dim iTop1, iLeft1, iWidth1, iHeight1 As Integer
            Dim iTop2, iLeft2, iWidth2, iHeight2 As Integer
            'On commence par s'assurer que la fenêtre visio est active
            oApp.ActiveWindow.Activate()
            System.Windows.Forms.Application.DoEvents()
             'On obtient les coordonnées de la fenêtre Visio principale
            oApp.Window.GetWindowRect(iLeft1, iTop1, iWidth1, iHeight1)
            'Celle de la fenêtre active dans celle de visio
            oApp.ActiveWindow.GetWindowRect(iLeft2, iTop2, iWidth2, iHeight2)
            'Capture écran
            Try
                'Il reste à trouver comment capturer seulement l'espace client, sans menu ni bordure ?
                Me.FullImage = VisioPrez.CaptureEcran(iLeft1 + iLeft2, iTop1 + iTop2, iWidth2, iHeight2)
                'Création de la vignette
                Me.ThumbnailImage = Me.FullImage.GetThumbnailImage(128, 72, Nothing, New IntPtr())
                'On met la vignette dans le ImageList2 pour le rendre disponible au TreeView de la fenêtre de navigation
            Catch
                'Pas supposé avoir d'erreur, mais peut-être en débugage ...
            End Try
        End If
    End Sub

    Public Sub Appliquer()
        ' Remettre l'affichage Visio dans l'état enregistré
        If Not oApp Is Nothing Then
            oApp.ShowToolbar = Me.ShowToolbar
            If Not oApp.ActiveDocument Is Nothing Then
                Try
                    'Sélectionner la bonne page
                    oApp.ActiveWindow.Page = oApp.ActiveDocument.Pages(Me.PageName)
                    oApp.ActiveWindow.Activate()
                    'Replacer la fenêtre courante
                    'oApp.ActiveWindow.WindowState = Me.WindowState
                    'oApp.ActiveWindow.SetWindowRect(Me.wLeft, Me.wTop, Me.wWidth, Me.wHeight)
                    'Éléments d'interface
                    oApp.ActiveWindow.ShowRulers = Me.ShowRulers
                    oApp.ActiveWindow.ShowScrollBars = Me.ShowScrollBars
                    oApp.ActiveWindow.ShowGuides = Me.ShowGuides
                    oApp.ActiveWindow.ShowPageTabs = Me.ShowPageTab
                    oApp.ActiveWindow.ShowGrid = Me.ShowGrid
                    'Replacer la zone visualisée
                    oApp.ActiveWindow.SetViewRect(Me.vLeft, Me.vTop, Me.vWidth, Me.vHeight)
                    'Appliquer la visibilité des calques
                    Dim iIndex As Integer = 0
                    For Each oLayer As Microsoft.Office.Interop.Visio.Layer In oApp.ActivePage.Layers
                        If Me.Calques.Contains(oLayer.Name) Then
                            If Me.Calques(oLayer.Name).Visible Then
                                oLayer.CellsC(visLayerVisible).FormulaU = "1"
                            Else
                                oLayer.CellsC(visLayerVisible).FormulaU = "0"
                            End If
                        End If
                    Next
                    System.Windows.Forms.Application.DoEvents()
                Catch Ex As Exception
                    'rien
                End Try
            End If
        End If
    End Sub
End Class
